/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package service;

import java.util.logging.Level;
import java.util.logging.Logger;
import po.Activity;
import po.User;
import java.util.List;
import dao.ActivityDao;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import po.Comment;
import po.Useractrelation;
/**
 *
 * @author HX
 */
public class ActivityServiceImpl implements ActivityService{
    private ActivityDao activityDao;

    public void setActivityDao(ActivityDao activityDao) {
        this.activityDao = activityDao;
    }
    
    public Activity getActivity(int activityId){
        return (Activity)activityDao.getActivity(activityId);
    }
    
    public void addActivity(Activity activity){
        try {
            activityDao.save(activity);
        } catch (Exception ex) {
            Logger.getLogger(ActivityServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public List<Activity> getUserFutureActivities(int userId){
        List list =  (List<Activity>)activityDao.getUserActivities(userId);
        Iterator it = list.iterator();
        Date now = new Date();
        List futuerActivities = new ArrayList();
        Activity a;
        while(it.hasNext()){
            a = (Activity)it.next();
            if(a.getStarttime().getTime() >= now.getTime()){
                futuerActivities.add(a);
            }
        }
        return futuerActivities;
    }

    public List<Activity> getUserHisoryActivities(int userId){
        List list =  (List<Activity>)activityDao.getUserActivities(userId);
        Iterator it = list.iterator();
        Date now = new Date();
        List historyActivities = new ArrayList();
        Activity a;
        while(it.hasNext()){
            a = (Activity)it.next();
            if(a.getStarttime().getTime() < now.getTime()){
                historyActivities.add(a);
            }
        }
        return historyActivities;
    }

    public List<Activity> getOrgFutureActivities(int orgId){
        List list =  (List<Activity>)activityDao.getUserActivities(orgId);
        Iterator it = list.iterator();
        Date now = new Date();
        List futuerActivities = new ArrayList();
        Activity a;
        while(it.hasNext()){
            a = (Activity)it.next();
            if(a.getStarttime().getTime() >= now.getTime()){
                futuerActivities.add(a);
            }
        }
        return futuerActivities;
    }

    public List<Activity> getOrgHistoryActivities(int orgId){
        List list =  (List<Activity>)activityDao.getOrgActivities(orgId);
        Iterator it = list.iterator();
        Date now = new Date();
        List historyActivities = new ArrayList();
        Activity a;
        while(it.hasNext()){
            a = (Activity)it.next();
            if(a.getStarttime().getTime() < now.getTime()){
                historyActivities.add(a);
            }
        }
        return historyActivities;
    }

    public List<Activity> getOrgActivities(int orgId){
        return (List<Activity>)activityDao.getOrgActivities(orgId);
    }
    
    public void deleteActivity(int activityId){
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List getActors(int activityId){
        return (List)activityDao.getActors(activityId);
    }

    public List getMembers(int orgId){
        return (List)activityDao.getMembers(orgId);
    }

    public void joinActivity(Useractrelation useractrelation)
    {
        activityDao.joinActivity(useractrelation);
    }

    public boolean isActor(int activityId, int userId){
        return activityDao.isActor(activityId, userId);
    }

    public void editActivityBehavior(int activityId, int userId, String behavior){
        activityDao.editActivityBehavior(activityId, userId, behavior);
    }
    
    public void editActivityScore(int activityId, int userId, int score){
        activityDao.editActivityScore(activityId, userId, score);
    }

    public List<Useractrelation> getUseractrelation(int activityId){
        return (List<Useractrelation>)activityDao.getUseractrelation(activityId);
    }

    public void addUseractrelations(List<Useractrelation> useractrelations){
        activityDao.addUseractrelations(useractrelations);
    }

    public void askForLeave(int activityId, int userId){
        activityDao.askForLeave(activityId, userId);
    }

    public List<Comment> getComments(int activityId){
        return (List<Comment>)activityDao.getComments(activityId);
    }

    public void addComment(int activityId, int userId, String content){
        activityDao.addComment(activityId, userId, content);
    }

    public void deleteComment(int commentId){
        activityDao.deleteComment(commentId);
    }

}
